/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import flightlifter.SensorSource;
import java.lang.reflect.*;
import java.awt.event.MouseEvent;
import org.lwjgl.opengl.GL11;

public class Button extends GUIComponent {

    public String title = null;
    //public PImage icon = null;
    public boolean enabled = true;

    public SensorSource generator = null;
    public int index;

    public Button(String title, GUIListener listener, SensorSource generator, int index) {
        this.title = title;
        this.listener = listener;
        this.generator = generator;
        this.index = index;


    }

    public Button(String title, GUIListener listener) {
        this(title,listener,null,0);
    }


    public void draw() {
        super.draw();

        GL11.glColor4f(parent.TEXT[0], parent.TEXT[1], parent.TEXT[2], parent.TEXT[3]);
        parent.font.drawString(locX + 5, locY, title, 1, 1);


    }

    public int mousePressed(int mouseX, int mouseY) {
        
        if (enabled) {
            //check if mouse pressed on this object
            if (mouseX > locX && mouseX < locX + cWidth) {
                if (mouseY < locY + cHeight && mouseY > locY) {

                    if (listener != null) {
                        try {
                           
                           // GUIEvent ge = new GUIEvent(title, 0, generator, index);
                            GUIEvent ge = new GUIEvent(title, 0);
                            //Method m = listener.getClass().getDeclaredMethod("GUIEventPerformed", new Class[]{ge.getClass()});
                            //m.invoke(listener, new Object[]{ge});
                            listener.GUIEventPerformed(ge);


                        } catch (Exception ex) {
                            System.out.println("Can't pass event message: " + ex);

                        }
                    }

                    return 1;
                }

            }
            return 0;
        }
        return 0;

    }

    public void setSize(int width, int height) {

        if (width > cWidth) {
            hborder = (width - cWidth) / 2;
            this.cWidth = width;
        }
        if (height > cHeight) {
            this.cHeight = height;
            vborder = (height - cHeight) / 2;
        }


    }
}
